package com.ybfenpan.controller.member.member;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Gong;
import com.ybfenpan.model.Member;
import com.ybfenpan.service.GongService;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Controller

public class MemberGongController extends BaseController {

    @Resource
    private MemberService memberService;

    @Resource
    private GongService gongService;

    @RequestMapping("member/member/tuGong.shtml")
    public String kuang(ModelMap model, HttpServletRequest request) throws Exception {

        Map formMap = getParameterMap(request);
        //传递的参数
        String loginName = MapUtil.getString(formMap, "loginName");

        Gong gong1 = null;

        Gong gong21 = null;
        Gong gong22 = null;

        Gong gong31 = null;
        Gong gong32 = null;
        Gong gong33 = null;
        Gong gong34 = null;

        Member member1 = null;

        Member member = ContextUtil.getMember();
        member = this.memberService.getByKey(member.getId());
        if (loginName == null || loginName.length() < 1) {
            gong1 = this.gongService.executGongByLoginName(member.getLoginName());
            member1 = this.memberService.getByKey(member.getId());

        } else {
            gong1 = this.gongService.executGongByLoginName(loginName);
            member1 = memberService.executMemberByLoginName(loginName);
            if (member1 == null) {
                if (member.getLevel() > 0) {
                    member1 = this.memberService.getByKey(member.getId());
                }

            }
        }

        request.setAttribute("member1", member1);

        if (gong1 != null) {
            //获得第二层
            Integer zuoId = gong1.getGongZuoId();
            Integer youId = gong1.getGongYouId();
            gong21 = gongService.getByKey(zuoId);
            gong22 = gongService.getByKey(youId);
        }


        //获得第三层
        if (gong21 != null) {

            Integer zuoId = gong21.getGongZuoId();
            gong31 = gongService.getByKey(zuoId);

            Integer youId = gong21.getGongYouId();
            gong32 = gongService.getByKey(youId);

            Member member21 = this.memberService.getByKey(gong21.getMemberId());
            request.setAttribute("member21", member21);

            if (gong31 != null) {
                Member member31 = this.memberService.getByKey(gong31.getMemberId());
                request.setAttribute("member31", member31);
            }

            if (gong32 != null) {
                Member member32 = this.memberService.getByKey(gong32.getMemberId());
                request.setAttribute("member32", member32);
            }
        }
        if (gong22 != null) {

            Integer zuoId = gong22.getGongZuoId();
            gong33 = gongService.getByKey(zuoId);

            Integer youId = gong22.getGongYouId();
            gong34 = gongService.getByKey(youId);

            Member member22 = this.memberService.getByKey(gong22.getMemberId());
            request.setAttribute("member22", member22);

            if (gong33 != null) {
                Member member33 = this.memberService.getByKey(gong33.getMemberId());
                request.setAttribute("member33", member33);
            }

            if (gong34 != null) {
                Member member34 = this.memberService.getByKey(gong34.getMemberId());
                request.setAttribute("member34", member34);
            }

        }


        request.setAttribute("gong1", gong1);
        request.setAttribute("gong21", gong21);
        request.setAttribute("gong22", gong22);
        request.setAttribute("gong31", gong31);
        request.setAttribute("gong32", gong32);
        request.setAttribute("gong33", gong33);
        request.setAttribute("gong34", gong34);

        return "member/member/tuGong";
    }


}
